package com.cleargroup.magic.cleara.c.command;

import android.content.Context;
import android.text.TextUtils;

import com.cleargroup.magic.cleara.c.StatFsUtil;

import java.io.File;
import java.util.List;
import java.util.Vector;


//Based on commit 2e807b4 Luis on 2015/10/20 0020 at 9:28 [All-In-One-Toolbox]
public class Scan {
    private static final String TAG = Scan.class.getSimpleName();
    private ScanListener listener = null;
    private boolean isCanceled = false;
    private boolean isComplete = false;
    private Context context = null;
    private List<String> ignores;

    public long mUsedSize;
    public long mScanSize;


    public Scan(Context context) {
        this.context = context;
    }

    public Scan(Context context, boolean isIgnore) {
        this.context = context;
    }

    public Scan setListener(ScanListener listener) {
        this.listener = listener;
        return this;
    }

    public boolean isCanceled() {
        return isCanceled;
    }

    public void bfs(String path) {
        if(TextUtils.isEmpty(path)) {
            return;
        }


        File root = new File(path);
        if (!root.exists()) {
            return;
        }

        long[] blockSize = StatFsUtil.statBlockSize(path);
        mUsedSize = blockSize[StatFsUtil.USED];

        Vector<File> queue = new Vector<File>();
        queue.addElement(root);

        while (queue.size() > 0) {
            if (isCanceled()) {
                return;
            }

            File node = queue.firstElement();
            queue.removeElement(node);
            if (node != null) {
                mScanSize += node.length();
            } else {
                continue;
            }

            if (node.isDirectory() && node.canRead()) {
                File[] files = node.listFiles();
                if (files == null) {
                    continue;
                }

                for (File file : files) {
                    if (isCanceled()) {
                        return;
                    }

                    if (file != null) {
                        mScanSize += file.length();
                    } else {
                        continue;
                    }

                    if (file.isDirectory()) {
                        if (!ignores.contains(file.getAbsolutePath())) {
                            queue.addElement(file);
                            if (listener != null && !isCanceled()) {
                                listener.onNodeScan(file);
                            }
                        }
                    } else {
                        if (listener != null && !isCanceled()) {
                            listener.onNodeScan(file);
                        }
                    }
                }
            } else {
                if (listener != null && !isCanceled()) {
                    listener.onNodeScan(node);
                }
            }
        }

        // if scan complete or cancel
        if (queue.size() == 0) {
            setComplete(true);
        }

        queue.removeAllElements();
        queue = null;
    }

    public void setCanceled(boolean isCanceled) {
        this.isCanceled = isCanceled;
        this.listener = null;
    }

    public boolean isComplete() {
        return isComplete;
    }

    public void setComplete(boolean isComplete) {
        this.isComplete = isComplete;
    }

    //递归求取目录apk文件个数 @Author LiuXiaoYi
    public int getFileCounts(File f){
        int size = 0;
        File[] files = f.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    size = size + getFileCounts(files[i]);
                } else if (files[i].getName().toLowerCase().endsWith(".apk")) {
                    size++;
                }
            }
        }
        return size;
    }

    public Context getContext() {
        return context;
    }
}
